<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use \think\Loader;
use \think\Db;
use \think\Request;

// +----------------------------------------------------------------------
// | XpCms
// +----------------------------------------------------------------------
// | Copyright (c) 2021-2099
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mercurio（Xp） <725668993@qq.com>
// +----------------------------------------------------------------------

/*
** 留言 控制器
*/

class Message extends Adminbase {
	private static $_message 	= null; 	// 数据表对象
	
	// 优先加载
	public function _initialize() {
		parent::_initialize();
		
		// 实例化数据表模型
		self::$_message 	= Loader::model('Message');
		
	}
	
	// 表单 列表
	public function index() {
	    
	    // 当前页
		$nowPage = 1;
		if(isset($_GET['page']) && $_GET['page']){
			$nowPage = input('param.page/d');
			$nowPage = $nowPage > 0 ? $nowPage : 1;
		}
		
		$where = array();
		
		if(input('param.keyword')){
		    $keyword = input('param.keyword');
		    $this -> assign('keyword', $keyword);
		    $where['name|phone|content']=array('like','%'.$keyword.'%');
		}
		
		// 每页显示条数，默认 10 条
		$pagesize=input('param.page_size')?input('param.page_size'):10;
		$this -> assign('pagesize', pagesize($pagesize));
		
		$lists = self::$_message -> messageLists($where, $nowPage, $pagesize);
		
		
		$this -> assign('lists', $lists['lists']);
		$this -> assign('page', $lists['page']);
	    
		
		
		return view('index');
	}
	
	
	// 编辑留言页面
	public function edit() {
		
		$id = input('param.id/d');
		if(!$id){
			$this -> error('参数错误');
		}
		
		$prefix=config('database.prefix');
        $tablename=$prefix.'message';
        // 去除数组获取到添加的
        $sp = array('id','name','phone','content','create_time','update_time','status','source','remark');
        $Fields=Db::getTableFields($tablename);
        for ($i = 0; $i < count($sp); $i++) {
            $key=array_search($sp[$i] ,$Fields);
            unset($Fields[$key]);
        }
        $Fields1 = array_values($Fields);
        $fields2 = array();
        // 获取字段的注释
        $comments = Db::query("show full columns from ".$tablename);
        for ($j = 0; $j < count($Fields1); $j++){
            $fields2[$j]['field'] = $Fields1[$j];
            for ($k = 0; $k < count($comments); $k++) {
                if($comments[$k]['Field'] == $Fields1[$j]){
                    $fields2[$j]['name'] = $comments[$k]['Comment'];
                }
            }
        }
        $this -> assign('fields', $fields2);
		
		$lists = self::$_message -> getMessageDetail($id);
		if(!$lists){
			$this -> error('参数错误');
		}
		
		$this -> assign('lists', $lists);
		return view('add');
	}
	
	// 保存编辑表单
	public function editsave() {
		
		$inputs = input('post.');
		
		$id = (int) $inputs['id'];
		if(!$id){
			$this -> error('参数错误');
		}
		
		// 使用模型验证器进行验证
// 		$this -> checkNameAdd($inputs);
		
		$find = self::$_message -> where(array('id' => $id)) -> value('id');
		if(!$find){
			$this -> error('参数错误');
		}
		
		// 处理时间
		$inputs['update_time'] = time();
		// 保存数据
		unset($inputs['id']);
		if(self::$_message -> save($inputs, array('id' => $id))){
			$this -> success('操作成功', 'index');
		}
		
		$this -> error('操作失败或没有数据可更新');
	}
	
	// 编辑表单状态
	public function editstatus() {
		
		$id = input('post.id/d');
		if(!$id){
		    return ['error'=>1,'msg'=>'参数错误'];
		}
		
		$result = self::$_message -> editStatus($id);
		if($result){
		    return ['error'=>0,'msg'=>'操作成功'];
		}
	    return ['error'=>1,'msg'=>'操作失败'];
	}
	
	// 删除表单
	public function deletes() {
		
		$id = input('post.id/d');
		if(!$id){
			return ['error'=>1,'msg'=>'参数错误'];
		}
		
		$result = self::$_message -> deletes($id);
		if($result['status'] == 1){
		    return ['error'=>0,'msg'=>'删除成功!'];
		}
        return ['error'=>1,'msg'=>$result['msg']];
	}
	
	//批量删除表单
    public function selectDel(){
        $id = input('post.id');
        if(!$id){
			return ['error'=>1,'msg'=>'ID不存在'];
		}
		$ids = explode(',', $id);
		for ($i = 0; $i < count($ids); $i++) {
	        $result = self::$_message -> deletes($ids[$i]);
    		if($result['status'] == 1){
    		}else{
    	        return ['error'=>1,'msg'=>$result['msg']];
    		}
		}
		return ['error'=>0,'msg'=>'删除成功!'];
    }
	
		// 验证新增数据 name 是否重复添加
	private function checkNameAdd($data) {
		$checkData = array('fname' => $data['fname']);
		$find = self::$_form -> where($checkData) -> find();
		if($find){
			$this -> error('同样的记录已经存在');
		}
		return true;
	}
	
	public function field(){

        $sysfield = array('name','phone','content');

        $list = Db::name('formfield')
            ->where("fcode",'=',input('id'))
            ->order('id asc')
            ->select();
        foreach ($list as $k=>$v){

            if(in_array($v['field'],$sysfield)){
                $list[$k]['delStatus']=1;//不可删除
            }else{
                $list[$k]['delStatus']=0;//可删除
            }
        }
        $this->view->assign('list'     , $list);
        $this->view->assign('fcode' , input('id'));
        return $this->view->fetch();
    }
    
	
}
